package com.example.xloong.mymvvm.viewmodel;

import com.example.xloong.mymvvm.adapter.NewsAdapter;
import com.example.xloong.mymvvm.base.BaseLoadListener;
import com.example.xloong.mymvvm.base.IBaseView;
import com.example.xloong.mymvvm.bean.NewsBean;
import com.example.xloong.mymvvm.bean.NewsBeanSimple;
import com.example.xloong.mymvvm.constant.MainConstant;
import com.example.xloong.mymvvm.model.NewsModel;
import com.example.xloong.mymvvm.model.NewsModelImpl;

import java.util.List;

/**
 * Created by Xloong on 2018/1/26.
 */

public class NewsViewmodel implements BaseLoadListener<NewsBeanSimple>{

    private static final String TAG = "NewsViewmodel";
    private NewsModel newsModel;
    private IBaseView iBaseView;
    private int currPage= 1;
    private int loadType;
    private String id  ="4";
    private NewsAdapter newsAdapter;


    public NewsViewmodel(IBaseView iBaseView, NewsAdapter newsAdapter) {
        this.iBaseView = iBaseView;
        this.newsAdapter = newsAdapter;
        newsModel = new NewsModelImpl();
        getNewsData();
    }



    /**
     *
     * 第一次获取新闻数据
     *
     */


    public void getNewsData() {
       loadType = MainConstant.LoadData.FIRST_LOAD;
        newsModel.loadNewsData(currPage,this,id);
    }

    /**
     *
     * 获取下拉刷新的数据
     *
     */

    public void loadRefreshData(){
        loadType  = MainConstant.LoadData.REFRESH;
        currPage = 1;
        newsModel.loadNewsData(currPage,this,id);
    }

    /**
     *
     * 获取上拉加载更多的数据
     *
     */
    public void loadMoreData(){
        loadType = MainConstant.LoadData.LOAD_MORE;
        currPage++;
        newsModel.loadNewsData(currPage,this,id);

    }




    @Override
    public void loadSuccess(List<NewsBeanSimple> lsit) {
        if (currPage > 1){
            //上拉加载数据
            newsAdapter.loadMoreData(lsit);
        }else {
            //第一次加载或者下拉刷新的数据
            newsAdapter.refreshData(lsit);
        }
    }

    @Override
    public void loadFailure(String message) {
        // 加载失败后的提示
        if (currPage > 1){
            //加载失败需要回到加载之前的页数
            currPage -- ;
        }
        iBaseView.loadFailure(message);

    }

    @Override
    public void loadStart() {
        iBaseView.loadStart(loadType);
    }

    @Override
    public void loadComplete() {
        iBaseView.loadComplete();
    }


}
